package com.shiro.test.mvc.controller;

import com.shiro.test.mvc.pojo.LeaveApply;
import com.shiro.test.mvc.pojo.User;
import com.shiro.test.mvc.service.ILeaveApplyService;
import com.shiro.test.mvc.util.Constants;
import com.shiro.test.mvc.util.MessageUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2020/12/1 0001.
 */
@Controller
@RequestMapping("/system/leave.html")
public class LeaveApplyController {

    @Autowired
    private ILeaveApplyService leaveApplyService;

    @RequestMapping(params = "act=page")
    public String leaveList(){
        return "leave_list";
    }
    @RequestMapping(params = "act=go_edit")
    public String go_edit(){
        return "leave_edit";
    }

    @RequestMapping(params = "act=edit")
    @ResponseBody
    public Map<String, Object> doEdit(LeaveApply leaveApply){
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        User user = (User) session.getAttribute(Constants.SESSION_USER);
        leaveApply.setUser(user);
        if (leaveApply.getId() == null){
            leaveApplyService.addLeaveApply(leaveApply);
        }else {
            leaveApplyService.updateTaskResubmit(leaveApply);
        }

        return MessageUtil.getSuccessResult("提交成功");
    }

    @RequestMapping(params = "act=list")
    @ResponseBody
    public List<LeaveApply> list(){
        LeaveApply leaveApply = new LeaveApply();
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        User user = (User) session.getAttribute(Constants.SESSION_USER);
        leaveApply.setUser(user);
        return leaveApplyService.getLeaveList(leaveApply);
    }

}
